package com.cn.xiaonuo.slbfinances.slredbao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.xiaonuo.common.constant.SystemConstant;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.common.utils.StringUtils;
import com.cn.xiaonuo.dbs.core.annotation.DataSource;
import com.cn.xiaonuo.slbfinances.slredbao.entity.SlBSystemConfig;
import com.cn.xiaonuo.slbfinances.slredbao.entity.SlBSystemLogs;
import com.cn.xiaonuo.slbfinances.slredbao.mapper.CmAgentShopMapper;
import com.cn.xiaonuo.slbfinances.slredbao.mapper.SlSystemConfigMapper;
import com.cn.xiaonuo.slbfinances.slredbao.mapper.SlBSystemLogsMapper;
import com.cn.xiaonuo.slbfinances.slredbao.service.IRedBaoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class RedBaoServiceImpl extends ServiceImpl<CmAgentShopMapper,String> implements IRedBaoService {
    @Resource
    private CmAgentShopMapper cmAgentShopMapper;
    @Resource
    private SlSystemConfigMapper slSystemConfigMapper;
    @Resource
    private SlBSystemLogsMapper slBSystemLogsMapper;

    //股权兑换记录后台
    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage queryExchangeList(String userId,String phone,String beginDate,String endDate,Integer pageNum,Integer pageSize) {
        BusinessMessage<Object> message = new BusinessMessage<>();
        try {
            //1.设置默认值
            if (null == pageNum || pageNum <= 1) {
                pageNum = 1;
            }
            if (null == pageSize || pageSize <= 1) {
                pageSize = 10;
            }

            //2.时间格式转换
            if(StringUtils.isNotBlank(beginDate)){
                beginDate = SystemConstant.SDF.format(new Date(new Long(beginDate)));
            }
            if(StringUtils.isNotBlank(endDate)){
                endDate = SystemConstant.SDF.format(new Date(new Long(endDate)));
            }

            //3.分页查询
            PageHelper.startPage(pageNum, pageSize);
            List<Map<String, Object>> headmanList = cmAgentShopMapper.queryExchangeList(userId,phone,beginDate,endDate);
            PageInfo<Map<String, Object>> data = new PageInfo<>(headmanList);

            message.setData(data);
            message.setMessage("查询成功");
            message.setSuccess(true);
            return message;
        } catch (Exception e) {
            e.printStackTrace();
            message.setMessage("查询失败");
            message.setSuccess(false);
            return message;
        }
    }

    //股权兑换参数--客服修改
    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage saveGqSystemConfig(String userId,String shareRate,String monthMax,String dayMax,String shareRule){
        BusinessMessage<Object> message = new BusinessMessage<>();
        try {
            //1.验证必填参数不能为空
            if(StringUtils.isBlank(userId)){
                message.setMessage("修改失败，必填参数userId不能为空");
                message.setSuccess(false);
                return message;
            }

            //2.兑换比例
            if(StringUtils.isNotBlank(shareRate)){
                QueryWrapper queryWrapper = new QueryWrapper(new SlBSystemConfig(){{
                    setName("GQ_SHARE_RATE");
                }});
                SlBSystemConfig slSystemConfig = slSystemConfigMapper.selectOne(queryWrapper);
                slSystemConfig.setContent(shareRate);
                slSystemConfig.setUpdatedAt(new Date());
                slSystemConfigMapper.updateById(slSystemConfig);

                //记录日志
                SlBSystemLogs slSystemLogs = new SlBSystemLogs();
                slSystemLogs.setUserid(Integer.parseInt(userId));
                slSystemLogs.setType(true);
                slSystemLogs.setUsername("后台人员");
                slSystemLogs.setAction("股权参数修改");
                slSystemLogs.setContent("股权兑换比例修改为："+shareRate);
                slSystemLogs.setTitle("股权参数修改");
                slSystemLogs.setCreateTime(1);
                slSystemLogs.setCreatedAt(new Date());
                slSystemLogs.setUpdatedAt(new Date());
                slBSystemLogsMapper.insert(slSystemLogs);
            }

            //3.当月最大值
            if(StringUtils.isNotBlank(monthMax)){
                QueryWrapper queryWrapper = new QueryWrapper(new SlBSystemConfig(){{
                    setName("GQ_MONTH_MAX_SHARES");
                }});
                SlBSystemConfig systemConfig = slSystemConfigMapper.selectOne(queryWrapper);
                systemConfig.setContent(monthMax);
                systemConfig.setUpdatedAt(new Date());
                slSystemConfigMapper.updateById(systemConfig);

                //记录日志
                SlBSystemLogs slSystemLogs = new SlBSystemLogs();
                slSystemLogs.setUserid(Integer.parseInt(userId));
                slSystemLogs.setType(true);
                slSystemLogs.setUsername("后台人员");
                slSystemLogs.setAction("股权参数修改");
                slSystemLogs.setContent("股权兑换月最大值修改为："+monthMax);
                slSystemLogs.setTitle("股权参数修改");
                slSystemLogs.setCreateTime(1);
                slSystemLogs.setCreatedAt(new Date());
                slSystemLogs.setUpdatedAt(new Date());
                slBSystemLogsMapper.insert(slSystemLogs);
            }

            //4.当日最大值
            if(StringUtils.isNotBlank(dayMax)){
                QueryWrapper queryWrapper = new QueryWrapper(new SlBSystemConfig(){{
                    setName("GQ_DAY_MAX_SHARES");
                }});
                SlBSystemConfig systemConfig = slSystemConfigMapper.selectOne(queryWrapper);
                systemConfig.setContent(dayMax);
                systemConfig.setUpdatedAt(new Date());
                slSystemConfigMapper.updateById(systemConfig);

                //记录日志
                SlBSystemLogs slSystemLogs = new SlBSystemLogs();
                slSystemLogs.setUserid(Integer.parseInt(userId));
                slSystemLogs.setType(true);
                slSystemLogs.setUsername("后台人员");
                slSystemLogs.setAction("股权参数修改");
                slSystemLogs.setContent("股权兑换日最大值修改为："+dayMax);
                slSystemLogs.setTitle("股权参数修改");
                slSystemLogs.setCreateTime(1);
                slSystemLogs.setCreatedAt(new Date());
                slSystemLogs.setUpdatedAt(new Date());
                slBSystemLogsMapper.insert(slSystemLogs);
            }

            //5.规则
            if(StringUtils.isNotBlank(shareRule)){
                QueryWrapper queryWrapper = new QueryWrapper(new SlBSystemConfig(){{
                    setName("GQ_DAY_MAX_SHARES");
                }});
                SlBSystemConfig systemConfig = slSystemConfigMapper.selectOne(queryWrapper);
                systemConfig.setContent(shareRule);
                systemConfig.setUpdatedAt(new Date());
                slSystemConfigMapper.updateById(systemConfig);

                //记录日志
                SlBSystemLogs slSystemLogs = new SlBSystemLogs();
                slSystemLogs.setUserid(Integer.parseInt(userId));
                slSystemLogs.setType(true);
                slSystemLogs.setUsername("后台人员");
                slSystemLogs.setAction("股权参数修改");
                slSystemLogs.setContent("股权兑换规则修改为："+shareRule);
                slSystemLogs.setTitle("股权参数修改");
                slSystemLogs.setCreateTime(1);
                slSystemLogs.setCreatedAt(new Date());
                slSystemLogs.setUpdatedAt(new Date());
                slBSystemLogsMapper.insert(slSystemLogs);
            }

            message.setMessage("修改成功");
            message.setSuccess(true);
            return message;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
            message.setMessage("修改失败");
            message.setSuccess(false);
            return message;
        }
    }
}
